package com.woniu.cloud.storeassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TStorePurchaseCar;
import com.woniu.cloud.entity.TStoreServiceMiddle;
import com.woniu.cloud.storeassembly.mapper.TStorePurchaseCarMapper;
import com.woniu.cloud.storeassembly.service.ITStorePurchaseCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 店铺进货购物车表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-22
 */
@Service
@Slf4j
public class TStorePurchaseCarServiceImpl extends ServiceImpl<TStorePurchaseCarMapper, TStorePurchaseCar> implements ITStorePurchaseCarService {


    @Resource
    private TStorePurchaseCarMapper storePurchaseCarMapper;


    @Override
    public Result getStorePurchaseCarByStoreId(Integer storeId) {
        log.info("查询->id:{}",storeId);
        QueryWrapper<TStorePurchaseCar> wrapper = new QueryWrapper<>();
        wrapper.eq("store_purchase_car_store_id",storeId);
        List<TStorePurchaseCar> storePurchaseCar = storePurchaseCarMapper.selectList(wrapper);
        log.info("结果->{}",storePurchaseCar);
        if (null != storePurchaseCar) {
            return Result.success("存在",storePurchaseCar);
        }
        return Result.fail("不存在",null);

    }

    @Override
    public Result insertTStorePurchaseCar(TStorePurchaseCar storePurchaseCar) {
        QueryWrapper<TStorePurchaseCar> wrapper = new QueryWrapper<>();
        wrapper.eq("store_purchase_car_store_id",storePurchaseCar.getStorePurchaseCarStoreId());
        wrapper.eq("store_purchase_car_good_id",storePurchaseCar.getStorePurchaseCarGoodId());
        List<TStorePurchaseCar> cars = storePurchaseCarMapper.selectList(wrapper);
        if (0!=cars.size()) {
            Integer num=cars.get(0).getStorePurchaseCarNum()+storePurchaseCar.getStorePurchaseCarNum();
            UpdateWrapper<TStorePurchaseCar> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("store_purchase_car_id",cars.get(0).getStorePurchaseCarId())
                    .set("store_purchase_car_num",num)
                    .set("store_purchase_car_total",num*storePurchaseCar.getStorePurchaseCarGoodPrice())
                    .set("store_purchase_car_status",storePurchaseCar.getStorePurchaseCarStatus());
            int update = storePurchaseCarMapper.update(null, updateWrapper);
            if (0!=update) {
                log.info("添加成功->{}",storePurchaseCar);
                return Result.success("添加成功",null);
            }
        }else{
            storePurchaseCar.setStorePurchaseCarTotal(storePurchaseCar.getStorePurchaseCarNum()*storePurchaseCar.getStorePurchaseCarGoodPrice());
            int insert = storePurchaseCarMapper.insert(storePurchaseCar);
            if (0!=insert) {
                log.info("添加成功->{}",storePurchaseCar);
                return Result.success("添加成功",null);
            }
        }
        return Result.fail("添加sb",null);
//        return Result.fail("添加失败",null);
    }

    @Override
    public Result updateStorePurchaseCar(TStorePurchaseCar storePurchaseCar) {
        UpdateWrapper<TStorePurchaseCar> wrapper = new UpdateWrapper<>();
        Double total=storePurchaseCar.getStorePurchaseCarNum()*storePurchaseCar.getStorePurchaseCarGoodPrice();
        storePurchaseCar.setStorePurchaseCarTotal(total);
        wrapper.eq("store_purchase_car_id",storePurchaseCar.getStorePurchaseCarId())
                .set(null!=storePurchaseCar.getStorePurchaseCarGoodId(),"store_purchase_car_good_id",storePurchaseCar.getStorePurchaseCarGoodId())
                .set(null!=storePurchaseCar.getStorePurchaseCarStoreId(),"store_purchase_car_store_id",storePurchaseCar.getStorePurchaseCarStoreId())
                .set(null!=storePurchaseCar.getStorePurchaseCarGoodPrice(),"store_purchase_car_good_price",storePurchaseCar.getStorePurchaseCarGoodPrice())
                .set(null!=storePurchaseCar.getStorePurchaseCarNum(),"store_purchase_car_num",storePurchaseCar.getStorePurchaseCarNum())
                .set(null!=storePurchaseCar.getStorePurchaseCarTotal(),"store_purchase_car_total",storePurchaseCar.getStorePurchaseCarTotal())
                .set(null!=storePurchaseCar.getStorePurchaseCarStatus(),"store_purchase_car_status",storePurchaseCar.getStorePurchaseCarStatus());
        int insert = storePurchaseCarMapper.update(storePurchaseCar,wrapper);
        if (0!=insert) {
            log.info("修改成功->{}",storePurchaseCar);
            return Result.success("修改成功",null);
        }
        return Result.fail("修改失败",null);

    }

    @Override
    public Result deleteStorePurchaseCar(Integer storePurchaseCarId) {
        log.info("正在删除->storePurchaseCarId:{}",storePurchaseCarId);
        int isDelete = storePurchaseCarMapper.deleteById(storePurchaseCarId);
        if (1==isDelete) {
            log.info("删除成功->");
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);

    }
}
